<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form size="small" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="name">
      <el-input v-model="dataForm.name" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input v-model="dataForm.age" placeholder="年龄"></el-input>
    </el-form-item>
    <el-form-item label="身份证号" prop="idCard">
      <el-input v-model="dataForm.idCard" placeholder="身份证号"></el-input>
    </el-form-item>
    <el-form-item label="入职时间" prop="hireDate">
      <el-input v-model="dataForm.hireDate" placeholder="入职时间"></el-input>
    </el-form-item>
    <el-form-item label="工作时间" prop="workDate">
      <el-input v-model="dataForm.workDate" placeholder="工作时间"></el-input>
    </el-form-item>
    <el-form-item label="职称" prop="titles">
      <el-input v-model="dataForm.titles" placeholder="职称"></el-input>
    </el-form-item>
    <el-form-item label="登记表" prop="sheet">
      <el-input v-model="dataForm.sheet" placeholder="登记表"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="是否高工" prop="highLevel">
      <el-input v-model="dataForm.highLevel" placeholder="是否高工"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="工作简历" prop="resume">
      <el-input v-model="dataForm.resume" placeholder="工作简历"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" type="textarea" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          age: '',
          idCard: '',
          hireDate: '',
          workDate: '',
          titles: '',
          sheet: '',
          mobile: '',
          highLevel: '',
          status: '',
          resume: '',
          remark: '',
          createOn: '',
          createUserId: '',
          createBy: '',
          modifiedOn: '',
          modifiedUserId: '',
          modifiedBy: ''
        },
        dataRule: {
          name: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '年龄不能为空', trigger: 'blur' }
          ],
          idCard: [
            { required: true, message: '身份证号不能为空', trigger: 'blur' }
          ],
          hireDate: [
            { required: true, message: '入职时间不能为空', trigger: 'blur' }
          ],
          workDate: [
            { required: true, message: '工作时间不能为空', trigger: 'blur' }
          ],
          titles: [
            { required: true, message: '职称不能为空', trigger: 'blur' }
          ],
          sheet: [
            { required: true, message: '登记表不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          highLevel: [
            { required: true, message: '是否高工: 0：否   1：是不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态  0：禁用 1：正常不能为空', trigger: 'blur' }
          ],
          resume: [
            { required: true, message: '工作简历不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          createOn: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          modifiedOn: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          modifiedUserId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          modifiedBy: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/expert/expert/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.expert.name
                this.dataForm.age = data.expert.age
                this.dataForm.idCard = data.expert.idCard
                this.dataForm.hireDate = data.expert.hireDate
                this.dataForm.workDate = data.expert.workDate
                this.dataForm.titles = data.expert.titles
                this.dataForm.sheet = data.expert.sheet
                this.dataForm.mobile = data.expert.mobile
                this.dataForm.highLevel = data.expert.highLevel
                this.dataForm.status = data.expert.status
                this.dataForm.resume = data.expert.resume
                this.dataForm.remark = data.expert.remark
                this.dataForm.createOn = data.expert.createOn
                this.dataForm.createUserId = data.expert.createUserId
                this.dataForm.createBy = data.expert.createBy
                this.dataForm.modifiedOn = data.expert.modifiedOn
                this.dataForm.modifiedUserId = data.expert.modifiedUserId
                this.dataForm.modifiedBy = data.expert.modifiedBy
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/expert/expert/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'age': this.dataForm.age,
                'idCard': this.dataForm.idCard,
                'hireDate': this.dataForm.hireDate,
                'workDate': this.dataForm.workDate,
                'titles': this.dataForm.titles,
                'sheet': this.dataForm.sheet,
                'mobile': this.dataForm.mobile,
                'highLevel': this.dataForm.highLevel,
                'status': this.dataForm.status,
                'resume': this.dataForm.resume,
                'remark': this.dataForm.remark,
                'createOn': this.dataForm.createOn,
                'createUserId': this.dataForm.createUserId,
                'createBy': this.dataForm.createBy,
                'modifiedOn': this.dataForm.modifiedOn,
                'modifiedUserId': this.dataForm.modifiedUserId,
                'modifiedBy': this.dataForm.modifiedBy
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
